package com.using.http.imgservice;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class ModifyLocalHtmlImg {
	public static void main(String[] args) throws Exception {
		// 递归所有文件夹及其中文件
		File root = new File("F:\\torrent\\07月");
		List<Map<String, String>> files = new ArrayList<Map<String, String>>();
		showAllFiles(root,files);
		for (int i = 0; i < files.size(); i++) {
			Map<String, String> map = files.get(i);
			System.out.println(i + "/" + files.size() + "--开始处理图片下载操作");
			// 读取HTML文件内容
			String fileContext = getTxtContext(map.get("fullFilePath"));
			// 解析htmL中的图片地址
			List<String> listImgFiles = getFileImgPath(fileContext);
			for (int j = 0; j < listImgFiles.size(); j++) {
				String fileNetUrl = listImgFiles.get(j);
				String imgFileName = fileNetUrl.substring(fileNetUrl.lastIndexOf("/") + 1);
				// 下载文件
				String locaImgFilePath = map.get("filePath") + "\\" + imgFileName;
				File fileImg = new File(locaImgFilePath);
				downFile(fileNetUrl, fileImg);
				System.out.println("下载完成：" + locaImgFilePath);
			}
		}
	}

	/**
	 * 解析HTML获取图片地址
	 * @param fileContext
	 * @return
	 */
	private static List<String> getFileImgPath(String fileContext) {
		List<String> listImgFile = new ArrayList<String>();
		Document document = Jsoup.parse(fileContext);
		Elements elements = document.getElementsByTag("img");
		for (int i = 0; i < elements.size(); i++) {
			Element e = elements.get(i);
			listImgFile.add(e.attr("src"));
		}
		return listImgFile;
	}

	/**
	 * 递归读取文件
	 * @param dir
	 * @param files
	 * @throws Exception
	 */
	final static void showAllFiles(File dir, List<Map<String, String>> files) throws Exception {
		File[] fs = dir.listFiles();
		for (int i = 0; i < fs.length; i++) {
			if(fs[i].getName().lastIndexOf(".html") != -1) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("fullFilePath", fs[i].getAbsolutePath());
				map.put("filename", fs[i].getName());
				map.put("filePath", fs[i].getParent());
				files.add(map);
			}
			if (fs[i].isDirectory()) {
				try {
					showAllFiles(fs[i],files);
				} catch (Exception e) {
				}
			}
		}
	}
	
	
	
	/**
	 * 获取描述内容
	 * @param fileName
	 * @return
	 */
	public static String getTxtContext(String fileFullPath) {
		StringBuffer sbStr = new StringBuffer("");
		try {
			File file = new File(fileFullPath);
			BufferedReader br = new BufferedReader(new FileReader(file));
			
			String r = br.readLine();
			while (r != null) {
				sbStr.append(r);
				r = br.readLine();
			}
			
		} catch (Exception e) {
			return "";
		}
		return sbStr.toString();
	}
	
	
	
	/**
	 * 描述：sendUrl 发送URL的post请求
	 * @param urlStr
	 * @param params
	 */
	public static void downFile(String urlStr, File file) {
		try {
			URL realUrl = new URL(urlStr);
			HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
			URL url = new URL(urlStr);
			// 打开url连接
			conn = (HttpURLConnection) url.openConnection();
			// 设置url请求方式 ‘get’ 或者 ‘post’
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(30000);
			conn.setReadTimeout(30000);
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");// IE代理进行下载  
			// 发送
			
			/* ----------- 生成文件 ------------ */
			InputStream in = conn.getInputStream();
			FileOutputStream os = new FileOutputStream(file);
			byte[] buffer = new byte[4 * 1024];
			int read;
			while ((read = in.read(buffer)) > 0) {
				os.write(buffer, 0, read);
			}
			os.flush();
			os.close();
			in.close();

			if (conn != null) {
				// 关闭连接
				conn.disconnect();
			}
		} catch (Exception e) {
			//e.printStackTrace();
			System.out.println(e.getMessage() + "，文件写入出错！");
			if(file.exists()) {
				file.delete();
			}
		}
	}
}
